package de.koller.million.render2D.obj;

import de.koller.million.render.util.Buffer.IGenerate;
import de.koller.million.render2D.RenderRoot2D;
import de.koller.million.render2D.RenderStatus;
import de.koller.oceanFramework.gl.util.ParticleTexture;
import de.koller.oceanFramework.tree.Mother;
import de.koller.oceanFramework.tree.Sea3X;
import de.koller.oceanFramework.util.ColorIntUtil;
import java.util.Random;
import java.util.StringTokenizer;
import org.lwjgl.opengl.GL11;

public class Building extends Sea3X {
	
	@Mother private RenderRoot2D rroot;
	private static final Random rnd = new Random();
	
	private ParticleTexture texture;
	private ParticleTexture facadeLight;
	
	private final float x, y, w, h;

	public Building( int x, int y, int w, int h ) {
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
	}
	
	@Override
	protected void init() {
		super.init();
		
		texture = rroot.buffer.bufferP.getContent( "facade", new Facade() );
		facadeLight = rroot.buffer.bufferP.getContent( "facadeLight " + rnd.nextInt( 7 ), new FacadeLight() );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		if( rroot.getRenderStatus() == RenderStatus.Base ) {
			texture.bind();
			GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT );
			GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT );
			texture.draw2D( x-w/2, y-h/2, w, h, 0, 0, 0, 1, h/w );
		}
		if( rroot.getRenderStatus() == RenderStatus.Light ) {
			facadeLight.bind();
			GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
			GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP );
			facadeLight.draw2D( x-w/2, y-h/2, w, h, 1, 0, 0, 1, h/w );
		}
	}
	
	private static class FacadeLight implements IGenerate<ParticleTexture> {
		
		private int color( int n ) {
			switch( n ) {
				case 0: return 0xff_0000ff;
				case 1: return 0xff_00ff00;
				case 2: return 0xff_00ffff;
				case 3: return 0xff_ff0000;
				case 4: return 0xff_ff00ff;
				case 5: return 0xff_ffff00;
				case 6: return 0xff_ffffff;
			}
			throw new IllegalArgumentException( n + " no color!" );
		}
		
		@Override
		public ParticleTexture create( String key ) {
			int size = Facade.SIZE;

			ParticleTexture.MAG_FILTER = GL11.GL_NEAREST;
			ParticleTexture t = ParticleTexture.generate( size, size );
			t.clear( 0x00000000 );
			
			StringTokenizer st = new StringTokenizer( key );
			st.nextToken();
			int c = color( Integer.parseInt( st.nextToken() ) );
			
			for( int i = 0; i < size; i++ ) {
				t.setPixel( c, i, 0 );
				t.setPixel( c, 0, i );
				t.setPixel( c, size-1, i );
			}
			
			t.flush();
			return t;
		}
	
	}
	
	private static class Facade implements IGenerate<ParticleTexture> {
		private static final Random rnd = new Random();
		private static final int SIZE = 16;
		
		@Override
		public ParticleTexture create( String key ) {
			int size = SIZE;
			ParticleTexture.MAG_FILTER = GL11.GL_NEAREST;
			ParticleTexture t = ParticleTexture.generate( size, size );
			for( int x = 0; x < size; x++ )
				for( int y = 0; y < size; y++ ) {
					int f = 20 + rnd.nextInt( 50 );
					t.setPixel( ColorIntUtil.getIntColor( 0xff, f, f, f ), x, y );
				}
			
			t.flush();
			
			return t;
		}
	
	}
	
}
